package com.zpphis.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpphis.entity.DiseCategory;
import com.zpphis.entity.Disease;
import com.zpphis.entity.UserType;
import com.zpphis.service.IDiseCategoryService;
import com.zpphis.service.IDiseaseService;
import com.zpphis.service.IUserService;
import com.zpphis.service.IUserTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/dise")
public class DiseController {

    @Autowired
    private IDiseCategoryService diseCategoryService;
    @Autowired
    private IDiseaseService diseaseService;

    @RequestMapping("/table.json")
    public Map<String, Object> table(int page, int limit, @RequestParam(defaultValue = "") String queryString) {

        Page<Map<String, Object>> p = new Page<>();
        p.setSize(limit);
        p.setCurrent(page);

        Page<Map<String, Object>> ps = diseaseService.pageExtend(p, queryString);
        return new HashMap<String, Object>() {
            {
                put("code", 0);
                put("count", ps.getTotal());
                put("data", ps.getRecords());
            }
        };

    }


    @RequestMapping("/add.json")
    public Map<String, Object> add(Disease entity) {
        if (diseaseService.save(entity)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "新增失败！");
                }
            };
        }
    }

    @RequestMapping("/del.json")
    public Map<String, Object> del(int id) {
        if (diseaseService.removeById(id)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "删除失败！");
                }
            };
        }
    }

    @RequestMapping("/queryOne.json")
    public Disease queryOne(int id) {
        return diseaseService.getById(id);
    }

    @RequestMapping("/edit.json")
    public Map<String, Object> edit(Disease entity) {
        if (diseaseService.updateById(entity)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "修改失败！");
                }
            };
        }
    }

    @RequestMapping("/typeTable.json")
    public Map<String, Object> typeTable(int page, int limit, @RequestParam(defaultValue = "") String queryString) {

        Page<DiseCategory> p = new Page<>();
        p.setSize(limit);
        p.setCurrent(page);
        LambdaQueryWrapper<DiseCategory> qw = new LambdaQueryWrapper<>();
        qw.like(DiseCategory::getDicaName, queryString).or().like(DiseCategory::getDicaCode, queryString);
        Page<DiseCategory> ps = diseCategoryService.page(p, qw);
        return new HashMap<String, Object>() {
            {
                put("code", 0);
                put("count", ps.getTotal());
                put("data", ps.getRecords());
            }
        };

    }

    @RequestMapping("/typeAdd.json")
    public Map<String, Object> typeAdd(DiseCategory entity) {
        if (diseCategoryService.save(entity)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "新增失败！");
                }
            };
        }
    }

    @RequestMapping("/typeDel.json")
    public Map<String, Object> typeDel(int id) {
        if (diseCategoryService.removeById(id)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "删除失败！");
                }
            };
        }
    }

    @RequestMapping("/typeQueryOne.json")
    public DiseCategory typeQueryOne(int id) {
        return diseCategoryService.getById(id);
    }

    @RequestMapping("/typeEdit.json")
    public Map<String, Object> typeEdit(DiseCategory entity) {
        if (diseCategoryService.updateById(entity)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "修改失败！");
                }
            };
        }
    }
}
